package io.renren.controller;

import com.alibaba.fastjson.JSONArray;
import io.renren.entity.DemandManagementEntity;
import io.renren.entity.SysUserEntity;
import io.renren.service.DemandManagementService;
import io.renren.utils.PageUtils;
import io.renren.utils.QueryAuthority;
import io.renren.utils.R;
import io.renren.utils.ShiroUtils;
import io.renren.validator.ValidatorUtils;
import io.renren.validator.group.AddGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 行政用人需求管理
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-11 17:19:09
 */
@RestController
@RequestMapping("demandmanagement")
public class DemandManagementController extends AbstractController {
	@Autowired
	private DemandManagementService demandManagementService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("demandmanagement:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        QueryAuthority query = new QueryAuthority(params);

		List<DemandManagementEntity> demandManagementList = demandManagementService.queryList(query);
		int total = demandManagementService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(demandManagementList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("demandmanagement:info")
	public R info(@PathVariable("id") Integer id){
		DemandManagementEntity demandManagement = demandManagementService.queryObject(id);
		
		return R.ok().put("demandManagement", demandManagement);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("demandmanagement:save")
	public R save(@RequestBody DemandManagementEntity demandManagement){
		SysUserEntity user = (SysUserEntity) ShiroUtils.getSubject().getSession().getAttribute("user");
		demandManagement.setCompanyId(user.getCompanyId());
		demandManagement.setCompany(user.getCompany());
		demandManagement.setApplyDept(user.getDept());
		demandManagement.setApplyDeptId(user.getDeptNo());
		demandManagement.setCusername(user.getUsername());
		demandManagement.setCdate(new Date());
		ValidatorUtils.validateEntity(demandManagement, AddGroup.class);
		demandManagementService.save(demandManagement);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("demandmanagement:update")
	public R update(@RequestBody DemandManagementEntity demandManagement){
		ValidatorUtils.validateEntity(demandManagement, AddGroup.class);

		demandManagementService.update(demandManagement);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("demandmanagement:delete")
	public R delete(@RequestBody Integer[] ids){
		demandManagementService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 批准
	 */
	@RequestMapping("/approve")
	public R approve(@RequestBody DemandManagementEntity demandManagement){
		SysUserEntity user=getUser();
		String username=user.getUsername();
		demandManagementService.approve(demandManagement,username);

		return R.ok();
	}

	/**
	 * 拒绝
	 */
	@RequestMapping("/reject")
	public R reject(@RequestBody DemandManagementEntity demandManagement){
		SysUserEntity user=getUser();
		String username=user.getUsername();
		demandManagementService.reject(demandManagement,username);

		return R.ok();
	}


	@RequestMapping("/choose")
	@RequiresPermissions("demandmanagement:list")
	public R choosePerson(@RequestParam Map<String, Object> params){
		String idss = params.get("ids").toString();
		JSONArray ids = JSONArray.parseArray(idss);
		List<Map<String, Object>> result = demandManagementService.choosePerson(ids);
		PageUtils pageUtil = new PageUtils(result, result.size(), 100, 1);

		return R.ok().put("page", pageUtil);
	}

	@RequestMapping("/selected")
	@RequiresPermissions("demandmanagement:save")
	public R selected(@RequestBody JSONArray params){

		int i = demandManagementService.selectedEmp(params);
		if(i == 0){
			return R.error();
		}else{
			return R.ok();
		}
	}


	
}
